
#include "base.h"
#include "misc.h"
#include "compress.h"
#include "crypt.h"

#include <windows.h>
#include <sstream>
#include <fstream>
#include <vector>
#include <iostream>

BASE_TESTSUITE_BEGIN(encrypt)
BASE_TESTSUITE_END

BASE_TESTSUITE_BEGIN(compress)
BASE_TESTSUITE_END

BASE_TESTCASE( encrypt, 1 )
{
	rijndael aes;
	uint8_t key[16] = {1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13, 14, 15, 16};
	{
		PERFCOUNTER("encrypt:setkey128");
		aes.setkey(key, rijndael::KEYSIZE_128BITS);
	}
	
	std::ifstream ifs("input.txt", std::ios::binary);

	tutassert(ifs.is_open());

	std::ostringstream text;
	text<<ifs.rdbuf();
	ifs.close();

	char * buffer = new char[text.str().size() + 16];
	tutassert(buffer != NULL);

	size_t size = 0;
	{
		PERFCOUNTER("encrypt:encryptmem");
		aes.encryptmem(text.str().c_str(), text.str().size(), buffer, size);
	}

	std::ofstream ofs("output.dat", std::ios::binary);
	ofs.write(buffer, size);
	ofs.close();

	aes.decryptmem(buffer, size, buffer, size);

	std::string newtext(buffer, text.str().size());
	tutassert(text.str() == newtext);
}

BASE_TESTCASE( encrypt, 2 )
{
	rijndael aes;
	uint8_t key[24] = {
		1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12,
		13, 14, 15, 16,	17, 18, 19, 20, 21, 22, 23, 24
	};
	{
		PERFCOUNTER("encrypt:setkey192");
		aes.setkey(key, rijndael::KEYSIZE_192BITS);
	}
	
	std::ifstream ifs("input.txt", std::ios::binary);

	tutassert(ifs.is_open());

	std::ostringstream text;
	text<<ifs.rdbuf();
	ifs.close();

	char * buffer = new char[text.str().size() + 16];
	tutassert(buffer != NULL);

	size_t size = 0;
	{
		PERFCOUNTER("encrypt:encryptmem");
		aes.encryptmem(text.str().c_str(), text.str().size(), buffer, size);
	}

	std::ofstream ofs("output.dat", std::ios::binary);
	ofs.write(buffer, size);
	ofs.close();

	aes.decryptmem(buffer, size, buffer, size);

	std::string newtext(buffer, text.str().size());
	tutassert(text.str() == newtext);
}

BASE_TESTCASE( encrypt, 3 )
{
	rijndael aes;
	uint8_t key[32] = {
		1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13, 14, 15, 16,
		17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32
	};
	{
		PERFCOUNTER("encrypt:setkey256");
		aes.setkey(key, rijndael::KEYSIZE_256BITS);
	}
	
	std::ifstream ifs("input.txt", std::ios::binary);

	tutassert(ifs.is_open());

	std::ostringstream text;
	text<<ifs.rdbuf();
	ifs.close();

	char * buffer = new char[text.str().size() + 16];
	tutassert(buffer != NULL);

	size_t size = 0;
	{
		PERFCOUNTER("encrypt:encryptmem");
		aes.encryptmem(text.str().c_str(), text.str().size(), buffer, size);
	}

	std::ofstream ofs("output.dat", std::ios::binary);
	ofs.write(buffer, size);
	ofs.close();

	aes.decryptmem(buffer, size, buffer, size);

	std::string newtext(buffer, text.str().size());
	tutassert(text.str() == newtext);
}
BASE_TESTCASE( encrypt, 4 )
{
	rijndael aes;
	uint8_t key[16] = {1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13, 14, 15, 16};
	{
		PERFCOUNTER("encrypt:setkey128");
		aes.setkey(key, rijndael::KEYSIZE_128BITS);
	}
	
	std::ifstream ifs("output.dat", std::ios::binary);

	tutassert(ifs.is_open());

	std::ostringstream data;
	data<<ifs.rdbuf();
	ifs.close();

	char * buffer = new char[data.str().size() + 16];
	tutassert(buffer != NULL);

	unsigned int size = 0;
	{
		PERFCOUNTER("encrypt:decryptmem");
		aes.decryptmem(data.str().c_str(), data.str().size(), buffer, size);
	}

	std::ofstream ofs("input.txt", std::ios::binary);
	ofs.write(buffer, size);
	ofs.close();

	aes.encryptmem(buffer, size, buffer, size);
	
	std::string newtext(buffer, data.str().size());
	tutassert(data.str() == newtext);
}

BASE_TESTCASE( encrypt, 5 )
{
	rijndael aes;
	uint8_t key[24] = {
		1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12,
		13, 14, 15, 16,	17, 18, 19, 20, 21, 22, 23, 24
	};
	{
		PERFCOUNTER("encrypt:setkey192");
		aes.setkey(key, rijndael::KEYSIZE_192BITS);
	}
	
	std::ifstream ifs("output.dat", std::ios::binary);

	tutassert(ifs.is_open());

	std::ostringstream data;
	data<<ifs.rdbuf();
	ifs.close();

	char * buffer = new char[data.str().size() + 16];
	tutassert(buffer != NULL);

	unsigned int size = 0;
	{
		PERFCOUNTER("encrypt:decryptmem");
		aes.decryptmem(data.str().c_str(), data.str().size(), buffer, size);
	}

	std::ofstream ofs("input.txt", std::ios::binary);
	ofs.write(buffer, size);
	ofs.close();

	aes.encryptmem(buffer, size, buffer, size);
	
	std::string newtext(buffer, data.str().size());
	tutassert(data.str() == newtext);
}

BASE_TESTCASE( encrypt, 6 )
{
	rijndael aes;
	uint8_t key[32] = {
		1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13, 14, 15, 16,
		17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32
	};
	{
		PERFCOUNTER("encrypt:setkey256");
		aes.setkey(key, rijndael::KEYSIZE_256BITS);
	}
	
	std::ifstream ifs("output.dat", std::ios::binary);

	tutassert(ifs.is_open());

	std::ostringstream data;
	data<<ifs.rdbuf();
	ifs.close();

	char * buffer = new char[data.str().size() + 16];
	tutassert(buffer != NULL);

	unsigned int size = 0;
	{
		PERFCOUNTER("encrypt:decryptmem");
		aes.decryptmem(data.str().c_str(), data.str().size(), buffer, size);
	}

	std::ofstream ofs("input.txt", std::ios::binary);
	ofs.write(buffer, size);
	ofs.close();

	aes.encryptmem(buffer, size, buffer, size);
	
	std::string newtext(buffer, data.str().size());
	tutassert(data.str() == newtext);
}


#include "quicklz.h"

BASE_TESTCASE( compress, 1 )
{
    FILE *ifile, *ofile;
    char *file_data, *compressed;
    size_t d, c;
	qlz_state_compress *state_compress = (qlz_state_compress *)malloc(sizeof(qlz_state_compress));

    ifile = fopen("input.txt", "rb");
	ofile = fopen("output.compress", "wb");

    file_data = (char*) malloc(10000);

    // allocate "uncompressed size" + 400 bytes for the destination buffer where 
    // "uncompressed size" = 10000 in worst case in this sample demo
    compressed = (char*) malloc(10000 + 400); 

    // allocate and initially zero out the states. After this, make sure it is
    // preserved across calls and never modified manually
	memset(state_compress, 0, sizeof(qlz_state_compress)); 
 
    // compress the file in random sized packets
    while((d = fread(file_data, 1, 8192, ifile)) != 0)
    {
		{
			PERFCOUNTER("compress:Encode");
			c = qlz_compress(file_data, compressed, d, state_compress);
		}
        //printf("%u bytes compressed into %u\n", (unsigned int)d, (unsigned int)c);

        // the buffer "compressed" now contains c bytes which we could have sent directly to a 
        // decompressing site for decompression
        fwrite(compressed, c, 1, ofile);
    }
    fclose(ifile);
    fclose(ofile);
	free(file_data);
	free(compressed);
}
//------------------------------------------------------------------------------

